import React, { Component } from 'react';
import {
  StyleSheet,
  View,
  Modal,
  Text,
  TouchableOpacity,
  PixelRatio,
  TextInput,
  Keyboard,
} from 'react-native';
import {
  mainBgColorLightGray,
  mainBgColorWhite,
  fontColorDeepRed,
  fontColorBlack,
  fontColorLightGray,
  fontColorCoffee,
  screenWidth,
  px2dp,
  screenHeight,
  splitLineColorLightGray,
  fontColorSecDeepGray,
  mainBgColorBlack,
  isAndroid,
} from '../../../../../common/styles';
import { Button, msg } from '../../../../../common';
import validate from '../../../../../common/validate';

const { Submit } = Button;

export default class SourceContact extends Component {
  constructor(props) {
    super(props);
    this.state = {
      linkMan: props.linkMan || '', // 联系人
      linkMobile: props.linkMobile || '', // 联系人电话
      keyboardHeight: '', // 键盘高度
    };
  }

  componentDidMount() {
    // 监听键盘
    this.keyboardDidShowListener = Keyboard.addListener(
      'keyboardWillShow',
      this._keyboardDidShow.bind(this),
    );
    this.keyboardDidHideListener = Keyboard.addListener(
      'keyboardWillHide',
      this._keyboardDidHide.bind(this),
    );
  }

  componentWillUnmount() {
    this.keyboardDidShowListener.remove();
    this.keyboardDidHideListener.remove();
  }

  render() {
    const { onChangeModal, onChangeVal, linkType } = this.props;
    const { linkMan, linkMobile } = this.state;
    return (
      <Modal
        animationType='slide'
        transparent
        visible
        onRequestClose={() => {
          onChangeModal({ type: 'sourceContactIsShow', val: false });
        }}
        onShow={() => {}}
      >
        <TouchableOpacity
          style={styles.mask}
          onPress={() => {
            onChangeModal({ type: 'sourceContactIsShow', val: false });
          }}
        />
        <View
          style={[
            styles.infoContainer,
            { marginBottom: Number(this.state.keyboardHeight) },
          ]}
        >
          <View style={styles.header}>
            <TouchableOpacity
              style={styles.cancel}
              onPress={() => {
                onChangeModal({ type: 'sourceContactIsShow', val: false });
              }}
            >
              <Text style={{ fontSize: px2dp(32), color: fontColorSecDeepGray }}>
                取消
              </Text>
            </TouchableOpacity>
            <Text
              style={{
                fontSize: px2dp(32),
                color: fontColorBlack,
                fontWeight: 'bold',
              }}
            >
              {linkType == 1 ? '装货联系人' : '卸货联系人'}
            </Text>
            <Submit
              text='确定'
              boxStyle={{
                width: px2dp(104),
                height: px2dp(60),
                borderRadius: px2dp(8),
                position: 'absolute',
                right: px2dp(28),
              }}
              disabled={false}
              onClick={() => {
                this.submit();
              }}
            />
          </View>
          <View style={styles.editContainer}>
            <View style={styles.inputContainer}>
              <Text style={styles.label}>联系人</Text>
              <TextInput
                placeholder='选填，请输入联系人姓名'
                value={linkMan}
                onChangeText={(val) => {
                  this.setState({ linkMan: val });
                }}
                style={styles.input}
              />
            </View>
            <View style={styles.inputContainer}>
              <Text style={styles.label}>手机号</Text>
              <TextInput
                placeholder='选填，请输入手机号'
                value={linkMobile}
                onChangeText={(val) => {
                  this.setState({ linkMobile: val });
                }}
                style={styles.input}
                maxLength={11}
                keyboardType={
                  isAndroid ? 'numeric' : 'decimal-pad'
                }
              />
            </View>
          </View>
          {/* 备注 */}
        </View>
      </Modal>
    );
  }

  // 提交
  submit = () => {
    const { onChangeContact, onChangeModal } = this.props;
    const { linkMan, linkMobile } = this.state;
    if (!linkMan.length) {
      msg.emit('app:tip', { text: '请输入联系人' });
      return;
    }
    if (!validate.phone.test(linkMobile)) {
      msg.emit('app:tip', { text: '请输入正确的手机号' });
      return;
    }
    onChangeModal({ type: 'sourceContactIsShow', val: false });
    onChangeContact({ linkMan, linkMobile });
  };

  _keyboardDidShow(e) {
    const { keyboardHeight } = this.state;
    if (keyboardHeight <= 0) {
      this.setState({
        keyboardHeight: e.startCoordinates.height,
      });
    }
  }

  _keyboardDidHide(e) {
    this.setState({
      keyboardHeight: 0,
    });
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    backgroundColor: mainBgColorLightGray,
    justifyContent: 'center',
  },
  mask: {
    width: screenWidth,
    height: screenHeight,
    backgroundColor: 'rgba(0,0,0,0.2)',
    position: 'absolute',
    left: 0,
    top: 0,
  },
  infoContainer: {
    width: screenWidth,
    maxHeight: 0.5 * screenHeight,
    backgroundColor: mainBgColorWhite,
    position: 'absolute',
    bottom: 0,
    left: 0,
    borderRadius: px2dp(12),
    overflow: 'hidden',
    zIndex: 10,
  },
  header: {
    width: screenWidth,
    height: px2dp(108),
    justifyContent: 'center',
    alignItems: 'center',
    borderBottomWidth: 1 / PixelRatio.get(),
    borderColor: splitLineColorLightGray,
  },
  cancel: {
    position: 'absolute',
    left: 0,
    top: 0,
    width: 80,
    height: '100%',
    justifyContent: 'center',
    alignItems: 'center',
  },

  editContainer: {
    paddingHorizontal: px2dp(32),
    paddingBottom: px2dp(100),
  },
  inputContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    marginTop: px2dp(48),
  },
  label: {
    width: px2dp(136),
    fontSize: px2dp(32),
    fontWeight: 'bold',
    color: fontColorBlack,
  },
  input: {
    width: px2dp(550),
    height: px2dp(88),
    backgroundColor: mainBgColorLightGray,
    borderRadius: px2dp(8),
  },
});
